package cn.team11.smarteye.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.TextView;

import cn.team11.sds.dayi.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.kyleduo.switchbutton.SwitchButton;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.team11.smarteye.activity.CateyeDeviceDetailActivity;
import cn.team11.smarteye.activity.callback.ConfigDeviceCallback;
import cn.team11.smarteye.activity.callback.ConfigDeviceResultCallback;
import cn.team11.smarteye.base.BaseDialog;
import cn.team11.smarteye.base.BaseFragment;
import cn.team11.smarteye.base.BasePresenter;
import cn.team11.smarteye.bean.CateyeInfoBean;
import cn.team11.smarteye.utils.AppLogger;
import io.reactivex.functions.Consumer;

/**
 * Created by Murphy on 2017/11/1.
 */

public class DetailConfigDeviceFragment extends BaseFragment implements CompoundButton.OnCheckedChangeListener, ConfigDeviceResultCallback {


    @BindView(R.id.device_charge)
    TextView mDeviceChargeTv;
    @BindView(R.id.device_network_name)
    TextView mNetworkNameTv;
    @BindView(R.id.disturb_switch)
    SwitchButton mDisturbSwitchBt;
    @BindView(R.id.detection_switch)
    SwitchButton mDetectionSwitchBt;
    @BindView(R.id.video_call_bt)
    FrameLayout mVideoCall;
    
    private ConfigDeviceCallback mCallback;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_device_config_device, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        AppLogger.i("DetailConfigDeviceFragment onViewCreated");
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    public static DetailConfigDeviceFragment newInstance(Bundle bundle) {
        DetailConfigDeviceFragment fragment = new DetailConfigDeviceFragment();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void init() {
        mDisturbSwitchBt.setOnCheckedChangeListener(this);
        mDetectionSwitchBt.setOnCheckedChangeListener(this);
        //注册自己，用于接收Activity的消息
        ((CateyeDeviceDetailActivity)getActivity()).registerConfigDeviceResultCallback(this);
        mCallback = (ConfigDeviceCallback)getActivity();

        if (mCallback != null){
            mCallback.getDeviceDetailInfo();
        }

        RxView.clicks(mVideoCall)
                .throttleFirst(3, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        if (mCallback != null){
                            mCallback.startVideoCallView();
                        }
                    }
                });

    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()){
            case R.id.disturb_switch:
                //设置勿扰模式
                break;
            case R.id.detection_switch:
                //人体侦测开关
                if (mCallback != null){
                    mCallback.enablePirSwitch(b);
                }
                break;
        }
    }


    @OnClick({R.id.device_delete_bt,
            R.id.device_share_bt,
            R.id.device_rename_bt,
            R.id.device_remote_bt})
    public void onClickAction(View view) {
        switch (view.getId()){
            case R.id.device_share_bt:
                //设备分享
                break;
            case R.id.device_rename_bt:
                //设备重命名
                EditDialogFragment dialogFragment = new EditDialogFragment();
                dialogFragment.show(getFragmentManager(), "rename");
                dialogFragment.setSubmitListener(new EditDialogFragment.InputSubmitListener() {
                    @Override
                    public void onSubmit(String name) {
                        if (mCallback != null){
                            mCallback.deviceRename(name);
                        }
                    }
                });
                break;
            case R.id.device_remote_bt:
                //远程重启
                Bundle b = new Bundle();
                b.putString("title", getString(R.string.device_remote_restart));
                b.putString("content", getString(R.string.restart_devices_tips));
                final TextDialogFragment restartDialog = TextDialogFragment.newInstance(b);
                restartDialog.show(getFragmentManager(), "restart");
                restartDialog.setOnBtClickListener(new BaseDialog.OnBtClickListener() {
                    @Override
                    public void onConfirmClick() {
                        if (mCallback != null){
                            mCallback.restartDevice();
                        }
                        restartDialog.dismiss();
                    }

                    @Override
                    public void onCancelClick() {
                        restartDialog.dismiss();
                    }
                });
                break;
            case R.id.device_delete_bt:
                //删除设备
                final DeleteDeviceDialogFragment deleteDeviceDialogFragment = new DeleteDeviceDialogFragment();
                deleteDeviceDialogFragment.show(getFragmentManager(), "delete");
                deleteDeviceDialogFragment.setOnBtClickListener(new BaseDialog.OnBtClickListener() {
                    @Override
                    public void onConfirmClick() {
                        if (mCallback != null){
                            mCallback.deleteDevice();
                        }
                        deleteDeviceDialogFragment.dismiss();
                    }

                    @Override
                    public void onCancelClick() {
                        deleteDeviceDialogFragment.dismiss();
                    }
                });
                break;
        }
    }


    @Override
    public void setPresenter(BasePresenter presenter) {

    }

    @Override
    public void onDeviceDetailResult(CateyeInfoBean info) {
        if (info != null) {
            //设置信息
            String chargeStr = new StringBuilder(getString(R.string.charge)).append(" ").append(info.getBattery_level()).append("%").toString();
            String wifiStr = new StringBuilder(getString(R.string.current_network)).append(" ").append(info.getWifi_config()).toString();
            mDeviceChargeTv.setText(chargeStr);
            mNetworkNameTv.setText(wifiStr);
            mDetectionSwitchBt.setChecked(info.getAlarm_enable()==1?true:false);
        }
    }

    @Override
    public void onRightButtonClick(View view) {

    }

    @Override
    public void onLeftButtonOnClick(View view) {

    }
}
